<template>
  <div class="login_container">
    LJ_VUE
    <div class="login_box">
      <div class="avater_box">
        <img src="../assets/login.png" />
      </div>
      <!-- 表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
            placeholder="请输入用户名 管理员用户: admin"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            show-password
            placeholder="请输入密码 管理员密码: 123456"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录($router.push)</el-button>
          <el-button type="info" @click="resetLoginForm">重置($refs)</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录表单对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单的验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async validate => {
        if (!validate) {
          return false
        } else {
          // const result = await this.$http.post('login', this.loginForm)
          // console.log(result.data)

          // 解构写法
          const { data: res } = await this.$http.post('login', this.loginForm)
          console.log(res.meta.status)
          if (res.meta.status === 200) {
            window.sessionStorage.setItem('token', res.data.token)
            this.$message({
              message: 'BINGO!!! 还以为你不记得自己的密码了',
              type: 'success'
            })
            this.$router.push('/home')
          } else {
            this.$message.error('健忘症又犯了???')
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: rgb(255, 255, 255);
  border-radius: 3px;
  box-shadow: 0 0 10px #dddddd;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avater_box {
    height: 130px;
    width: 130px;
    padding: 10xp;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -145%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eeeeee;
    }
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
  .login_form {
    width: 80%;
    position: absolute;
    left: 50%;
    top: 70%;
    transform: translate(-50%, -50%);
  }
}
</style>
